<template>
  <div>
    <!-- <el-input readonly  v-model="dealerName" >
      <i slot="suffix" class="el-input__icon el-icon-circle-plus" @click="openDia" ></i>
      <i slot="suffix" class="el-input__icon el-icon-error" @click="changeDE" ></i>
    </el-input> -->
    <el-dialog
      v-loading="loading"
      ref="dialog"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
      title="代理商选择"
      width="75%"
      @opened="searchHandle"
    >
      <!-- 查询条件 -->
      <el-form ref="searchForm" :inline="true" :model="search" size="mini" >
        <el-form-item prop="dealerCode" label="代理商代码">
          <el-input v-model="search.dealerCode" placeholder="代理商代码" class="search-input" clearable />
        </el-form-item>
        <el-form-item prop="dealerName" label="代理商名称">
          <el-input v-model="search.dealerName" placeholder="代理商名称" class="search-input" clearable />
        </el-form-item>
        <br>
        <div style="text-align:center">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-refresh" @click="resetForm('searchForm')">重置
            </el-button>
          </el-form-item>
        </div>
      </el-form>
      <div class="tableHead">
        <div class="tabletitle">代理商信息</div>
      </div>
      <!-- 用户列表 -->
      <BaseTable
        ref="table"
        highlight-current-row
        @chaneg-size="changeSizeHandle"
        @chaneg-page="changePageHandle"
        @current-change="select"
        @selection-change="handleSelectionVehicleChange"
        @row-dblclick="dblclick">
        <el-table-column label="序号" type="index" />
        <!-- <el-table-column v-if="allowRadio" type="selection" width="40" />-->
        <el-table-column type="selection" width="40" />
        <el-table-column prop="DEALER_CODE" label="代理商代码" min-width="100"/>
        <el-table-column prop="DEALER_NAME" label="代理商名称" min-width="200"/>
        <el-table-column prop="DEALER_SHORTNAME" label="代理商简称" min-width="150"/>
        <el-table-column prop="REMARK" label="备注" min-width="150"/>
      </BaseTable>
      <div style="text-align:center">
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="confirmHandle" >确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
          <el-button v-show="allowAll" type="primary" @click="confirmHandleAll" >选择 全部</el-button>
        </span>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllDealerInfo } from '@/api/admin/org/dealerInfo'
import filtercode from '@/components/Checkbox/filtercode'
import BaseTable from '@/components/BaseTable'
import formMixins from '@/mixins/formMixins'

export default {
  name: 'Dealer',
  components: {
    BaseTable,
    filtercode

  },
  mixins: [formMixins],
  props: {
    dealerName: String,
    dialogFormVisible: false,
    allowRadio: true,
    allowAll: false
  },
  data() {
    return {
      loading: false,
      multipleSelection: [],
      search: {
        limit: 10,
        pageNum: 1,
        dealerCode: '',
        dealerName: ''
      }
    }
  },
  computed: {},
  watch: {
    dialogFormVisible: function(val) {
      if (val) {
        this.search = {
          limit: 10,
          pageNum: 1,
          dealerCode: '',
          dealerName: ''
        }
        this.searchHandle()
      }
    }
  },
  mounted() {
    // this.searchHandle();
  },
  methods: {
    searchHandle() {
      // this.search.limit = 5
      const table = this.$refs['table']
      getAllDealerInfo(this.search)
        .then(res => {
          table.complete().filData(res)
        })
        .catch(err => {
          table.complete()
        })
    },
    handleSelectionVehicleChange(val) {
      if (val == null) {
        this.multipleSelection = []
      } else {
        this.multipleSelection = val
      }
    },
    openDia() {
      this.dialogFormVisible = true
      this.searchHandle()
    },
    cancel() {
      this.$emit('closeDealer', false)
      this.$emit('update:dialogFormVisible', false)
    },
    changeDE() {
      this.$emit('getDealerInfo', {})
    },
    // 发票订单选择
    select(val) {
      if (val == null) {
        this.multipleSelection = []
      } else {
        this.multipleSelection = val
      }
    },

    dblclick(row) {
      this.$emit('getDealerInfo', row)
      this.$emit('update:dialogFormVisible', false)
    },

    confirmHandle() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择数据!'
        })
      } else {
        this.$emit('getDealerInfo', this.multipleSelection)
        this.$emit('update:dialogFormVisible', false)
      }
    },
    confirmHandleAll() {
      this.loading = true
      this.search.limit = 10000
      getAllDealerInfo(this.search)
        .then(res => {
          this.$emit('getDealerInfo', res.rows)
          this.loading = false
          this.$emit('update:dialogFormVisible', false)
        })
        .catch(err => {
          this.$message({
            type: 'warning',
            message: '网络异常'
          })
        })
    }

  }
}
</script>

<style scoped>

.magnifier {
  float: right;
  position: absolute;
  right: 50px;
  top: 23px;
  cursor: pointer;
}
</style>
